package com.example.springdemo.controller;

import cn.hutool.core.util.IdUtil;
import com.example.springdemo.domain.HelloWorld;
import com.example.springdemo.domain.JsonData;
import com.example.springdemo.entity.Params;
import org.slf4j.Logger;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.context.Context;
import org.thymeleaf.spring5.SpringTemplateEngine;
import util.LogFileName;
import util.LoggerUtils;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @ClassName StaticHtml
 * @Description TODO
 * @Author MaChao
 * @Date 2022/2/25 14:32
 * @Version 1.0
 **/
@RestController
public class StaticHtml {
    Logger USER = LoggerUtils.logger(LogFileName.USER);
    @Resource
    HelloWorld helloWorld;
    @Resource
    JsonData jsonData;
    @Resource
    private SpringTemplateEngine templateEngine;

    @PostMapping("process")
    /* @RequestMapping("process")*/
    public HelloWorld process(@RequestBody Params jsonParam) throws IOException {
        System.out.println(jsonParam.toString());
        Context context = new Context();
        context.setVariable("name", jsonParam.getName());
        String fileName = IdUtil.objectId() + ".html";
        ApplicationHome home = new ApplicationHome(getClass());

        //File path=new File(ResourceUtils.getURL("classpath:").getPath());
        File path = home.getSource();
        File upload = new File(path.getAbsolutePath(), "static/html/");
        if (!upload.exists()) {
            upload.mkdirs();
            USER.info(upload.getAbsolutePath());
            //在开发测试模式时，得到地址为：{项目跟目录}/target/static/images/upload/
            //在打成jar正式发布时，得到的地址为:{发布jar包目录}/static/images/upload/
        }
        FileWriter fileWriter = new FileWriter(upload.getAbsolutePath() + "/" + fileName);
        templateEngine.process("world.html", context, fileWriter);
        helloWorld.setStart(1);
        helloWorld.setMag("页面生成成功");
        jsonData.setContent("http://localhost:8181/html/" + fileName);
        helloWorld.setData(jsonData);
        return helloWorld;
    }
}


//@RestController
//public class StaticHtml {
//    @Resource
//    private SpringTemplateEngine templateEngine;
//
//    @GetMapping("process")
//    public void process() throws IOException {
//        Context context = new Context();
//        context.setVariable("name", "thymeleaf 静态页面测试");
//        FileWriter fileWriter = new FileWriter("e:/html/thymeleaf.html");
//        templateEngine.process("world.html", context, fileWriter);
//    }
//}
//@RestController
//public class StaticHtml {
//    Logger USER = LoggerUtils.logger(LogFileName.USER);
//    @Resource
//    HelloWorld helloWorld;
//    @Resource
//    JsonData jsonData;
//    @Resource
//    private SpringTemplateEngine templateEngine;
//
//    @GetMapping("process")
//    @ResponseBody
//    public HelloWorld process() throws IOException {
//        Context context = new Context();
//        context.setVariable("name", "thymeleaf 静态页面测试");
//        String fileName = IdUtil.objectId() + ".html";
//        ApplicationHome home = new ApplicationHome(getClass());
//
//        //File path=new File(ResourceUtils.getURL("classpath:").getPath());
//        File path = home.getSource();
//        File upload = new File(path.getAbsolutePath(), "static/images/uplaod/");
//        if (!upload.exists()) {
//            upload.mkdirs();
//            USER.info(upload.getAbsolutePath());
//            //在开发测试模式时，得到地址为：{项目跟目录}/target/static/images/upload/
//            //在打成jar正式发布时，得到的地址为:{发布jar包目录}/static/images/upload/
//        }
//        FileWriter fileWriter = new FileWriter(upload.getAbsolutePath() + fileName);
//        templateEngine.process("world.html", context, fileWriter);
//        helloWorld.setStart(1);
//        helloWorld.setMag("页面生成成功");
//        jsonData.setContent("http://localhost:8181/xxxx/" + fileName);
//        helloWorld.setData(jsonData);
//        return helloWorld;
//    }
//}